package com.awesome.common.base.mvp

import android.app.Dialog
import android.content.DialogInterface
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.Window
import android.widget.Toast
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import com.awesome.common.util.ui.StatusBarUtil

/**
 * Author: JFangZ
 * Email: janezhang1901@163.com
 * Date:2021/4/8 11:32
 * Description:The base of DialogFragment.Recommend use DialogFragment instead of dialog.
 */
abstract class AbsDialogFragment : DialogFragment() {
    private var onDismissListener: OnDismissListener? = null

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Provided layoutId to setContentView()
     * @return The layoutId of this activity.
     */
    abstract fun initLayoutId(): Int

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do UI init operation,such as findViewByID,setAdapter and so on.
     */
    abstract fun initView()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:32
     * Description:Do setListener/setCallback operation.
     */
    abstract fun setListener()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2019/4/8 11:34
     * Description:Do data init operation,such as network request,read database,read file and so on.
     */
    abstract fun initData()

    override fun show(manager: FragmentManager, tag: String?) {
        manager.beginTransaction().remove(this).commit()
        super.show(manager, tag)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val mRootView = LayoutInflater.from(context).inflate(initLayoutId(), null)
        var dialog = Dialog(context!!)
        // 设置对话框无标题，该方法必须在 setContentView() 之前调用
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE)
        dialog.setContentView(mRootView)
        dialog.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        if (dialog.window != null) {
            if (immersive()) {
                // 使内容延伸到状态栏下
                StatusBarUtil.setStatusBarTranslucent(dialog.window, true)
                // 使状态栏透明
                StatusBarUtil.setStatusBarColor(dialog.window, Color.TRANSPARENT, false)
            } else {
                StatusBarUtil.setStatusBarTranslucent(dialog.window, false)
                StatusBarUtil.setStatusBarColor(dialog.window, initStatusBarColor(), false)
            }
            // 状态栏深色图标
            StatusBarUtil.setStatusBarStyle(dialog.window, initStatusBarDark())
        }
        dialog = customDialog(dialog)
        return dialog
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        initView()
        setListener()
        initData()
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        onDismissListener?.onDismiss(dialog)
    }

    /**
     * Description:如果对对话框样式有个性需求，可覆写该方法
     * Date:2019/2/19
     */
    open fun customDialog(dialog: Dialog): Dialog {
        return dialog
    }

    fun <T : View> findViewByID(id: Int): T {
        return dialog?.findViewById(id) as T
    }

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:39
     * Description:Make the activity immersive or not,if enable immersion,the status bar will transparent.
     * @return Returns true to enable immersion,false not to enable immersion.
     */
    open fun immersive(): Boolean = false

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:44
     * Description:Set the color of status bar.
     * @return Color's value.
     */
    open fun initStatusBarColor(): Int = 0xFF000000.toInt()

    /**
     * Author: JFangZ
     * Email: janezhang1901@163.com
     * Date:2021/4/8 11:45
     * Description:Set the mode of icon in status bar
     * @return Returns true to start dark mode,false to start light mode.
     */
    open fun initStatusBarDark(): Boolean = false

    fun toastShort(content: String) = Toast.makeText(context, content, Toast.LENGTH_SHORT).show()

    fun toastLong(content: String) = Toast.makeText(context, content, Toast.LENGTH_LONG).show()

    fun isShowing(): Boolean? = dialog?.isShowing

    fun show(fragmentManager: FragmentManager) = show(fragmentManager, javaClass.simpleName + hashCode())

    fun setOnDismissListener(onDismissListener: OnDismissListener): AbsDialogFragment {
        this.onDismissListener = onDismissListener
        return this
    }

    interface OnDismissListener {
        fun onDismiss(dialog: DialogInterface)
    }
}
